﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Web;
using System.Xml;
using System.Text.RegularExpressions;

namespace AliCMS.CourseModule.Bll
{
    public class Teacher : DbObjectBase, ITeacher
    {
        public Teacher(int instanceID, string lang, bool checkRight, string database)
            : base(instanceID, lang, checkRight, database)
        {
        }

        public override object ExecuteService(HttpContext context)
        {
            object result = null;

            string operation = context.Request.QueryString["Operation"];
            if (operation == "Add")
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                string value = null;
				value = context.Request.Form["Name"];
				if (value != null) item[Alias.Teacher.Name + Lang] = value;
				value = context.Request.Form["Picture"];
				if (value != null) item[Alias.Teacher.Picture] = value;
				value = context.Request.Form["Thumbnail"];
				if (value != null) item[Alias.Teacher.Thumbnail] = value;
				value = context.Request.Form["Commision"];
				if (value != null) item[Alias.Teacher.Commision] = value;
				value = context.Request.Form["Description"];
				if (value != null) item[Alias.Teacher.Description + Lang] = value;
				value = context.Request.Form["Recommend"];
				if (value != null) item[Alias.Teacher.Recommend] = value;
                value = context.Request.Form["UserID"];
                if (value != null) item[Alias.Teacher.UserID] = value;
                value = context.Request.Form["UserName"];
                if (value != null) item[Alias.Teacher.UserName] = value;
                value = context.Request.Form["UserNickname"];
                if (value != null) item[Alias.Teacher.UserNickname] = value;
                value = context.Request.Form["UserHead"];
                if (value != null) item[Alias.Teacher.UserHead] = value;
                value = context.Request.Form["IsChecked"];
				if (value != null) item[Alias.Teacher.IsChecked] = value;

                result = Add(item);
            }
            else if (operation == "Modify")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                Dictionary<string, object> changes = new Dictionary<string, object>();
                string value = null;
				value = context.Request.Form["Name"];
				if (value != null) changes[Alias.Teacher.Name + Lang] = value;
				value = context.Request.Form["Picture"];
				if (value != null) changes[Alias.Teacher.Picture] = value;
				value = context.Request.Form["Thumbnail"];
				if (value != null) changes[Alias.Teacher.Thumbnail] = value;
				value = context.Request.Form["Commision"];
				if (value != null) changes[Alias.Teacher.Commision] = value;
				value = context.Request.Form["Description"];
				if (value != null) changes[Alias.Teacher.Description + Lang] = value;
				value = context.Request.Form["Recommend"];
				if (value != null) changes[Alias.Teacher.Recommend] = value;
                value = context.Request.Form["UserID"];
                if (value != null) changes[Alias.Teacher.UserID] = value;
                value = context.Request.Form["UserName"];
                if (value != null) changes[Alias.Teacher.UserName] = value;
                value = context.Request.Form["UserNickname"];
                if (value != null) changes[Alias.Teacher.UserNickname] = value;
                value = context.Request.Form["UserHead"];
                if (value != null) changes[Alias.Teacher.UserHead] = value;
                value = context.Request.Form["IsChecked"];
				if (value != null) changes[Alias.Teacher.IsChecked] = value;

                Modify(id, changes);
            }
            else if (operation == "GetInnerIDFromGuid")
            {
                Guid guid = new Guid(context.Request.QueryString["ID"]);
                result = GetInnerIDFromGuid(guid);
            }
            else if (operation == "GetGuidFromInnerID")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);
                result = GetGuidFromInnerID(id);
            }
            else if (operation == "Delete")
            {
                string id = context.Request.QueryString["ID"];

                string[] ids = id.Split('|');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (String.IsNullOrEmpty(ids[i]))
                        continue;

                    if (i > 0)
                    {
                        Dal.EndWork();
                        Dal.BeginWork(InstanceID, Lang, CheckRight, String.Empty);
                    }

                    Delete(Convert.ToInt32(ids[i]));
                }
            }
            else if (operation == "Recycle")
            {
                string id = context.Request.QueryString["ID"];

                string[] ids = id.Split('|');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (String.IsNullOrEmpty(ids[i]))
                        continue;

                    Recycle(Convert.ToInt32(ids[i]));
                }
            }
            else if (operation == "Recover")
            {
                string id = context.Request.QueryString["ID"];

                string[] ids = id.Split('|');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (String.IsNullOrEmpty(ids[i]))
                        continue;

                    Recover(Convert.ToInt32(ids[i]));
                }
            }
            else if (operation == "List")
            {
                string value = null;

                int page = -1;
                value = context.Request.QueryString["Page"];
                if (value != null) page = Convert.ToInt32(value);

                int pageSize = -1;
                value = context.Request.QueryString["PageSize"];
                if (value != null) pageSize = Convert.ToInt32(value);

                string condition = context.Request.QueryString["Condition"];
                string sort = context.Request.QueryString["Sort"];

                result = List(page, pageSize, condition, sort);
            }
            else if (operation == "IncrementHitCount")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);
                IncrementHitCount(id);
            }
			else
            {
                throw new Exception("lang_header_param");
            }

            return result;
        }

        public int GetInnerIDFromGuid(Guid guid)
        {
            return Dal.GetInnerIDFromGuid(guid);
        }
        public Guid GetGuidFromInnerID(object id)
        {
            return Dal.GetGuidFromInnerID(id);
        }
        public Dictionary<string, object> Get(object id)
        {
            if (CheckRight)
            {
                Guid guid;
                if (id is Guid)
                    guid = (Guid)id;
                else
                    guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Get", guid);
            }

            return Dal.Get(id);
        }
        public Guid Add(Dictionary<string, object> item)
        {
            if (CheckRight)
            {
                ValidateRight("Add", Guid.Empty);
            }

            if (!item.ContainsKey(Alias.Teacher.ID) || item[Alias.Teacher.ID].ToString() == String.Empty)
                item[Alias.Teacher.ID] = Guid.NewGuid();
            else if (!(item[Alias.Teacher.ID] is Guid))
                item[Alias.Teacher.ID] = new Guid(item[Alias.Teacher.ID].ToString());

            if (item.ContainsKey(Alias.Teacher.UserID))
            {
                string userId = item[Alias.Teacher.UserID].ToString();
                if (userId == String.Empty || userId == Guid.Empty.ToString())
                    item[Alias.Teacher.UserID] = DBNull.Value;
                else
                    item[Alias.Teacher.UserID] = new Guid(userId);
            }

            Dictionary<string, string> user = Manager.User;

			//创建人
            item[Alias.Teacher.CreatorName] = user["Name"];
            item[Alias.Teacher.CreatorID] = new Guid(user["ID"]);

            Dal.Add(item);

            return (Guid)item[Alias.Teacher.ID];
        }
        public void Modify(int id, Dictionary<string, object> changes)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Modify", guid);
			}

            if (changes.ContainsKey(Alias.Teacher.UserID))
            {
                string userId = changes[Alias.Teacher.UserID].ToString();
                if (userId == String.Empty || userId == Guid.Empty.ToString())
                    changes[Alias.Teacher.UserID] = DBNull.Value;
                else
                    changes[Alias.Teacher.UserID] = new Guid(userId);
            }

            #region 更新人
            if (changes.ContainsKey(Alias.Teacher.UpdatorID))
            {
                changes[Alias.Teacher.UpdatorID] = new Guid(changes[Alias.Teacher.UpdatorID].ToString());
            }
            else
            {
                Dictionary<string, string> user = Manager.User;
                if (user != null)
                {
                    changes[Alias.Teacher.UpdatorName] = user["Name"];
                    changes[Alias.Teacher.UpdatorID] = new Guid(user["ID"]);
                }
            }

            if (changes.ContainsKey(Alias.Teacher.UpdateTime))
                changes[Alias.Teacher.UpdateTime] = Convert.ToDateTime(changes[Alias.Teacher.UpdateTime]);
			#endregion

			Dal.Modify(id, changes);
        }
        public void Recycle(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Recycle", guid);
            }

            Dictionary<string, object> changes = new Dictionary<string, object>();
            changes[Alias.Teacher.IsDeleted] = true;

            Dal.Modify(id, changes);
        }
        public void Recover(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Recover", guid);
            }

            Dictionary<string, object> changes = new Dictionary<string, object>();
            changes[Alias.Teacher.IsDeleted] = false;

            Dal.Modify(id, changes);
        }
        public void Delete(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Delete", guid);
            }

            Dictionary<string, object> item = Dal.Get(id);

            #region 删除缩略图
            string thumnail = item[Alias.Teacher.Thumbnail].ToString();
            if (thumnail.StartsWith("/") && File.Exists(Manager.MapPath(thumnail)))
                File.Delete(Manager.MapPath(thumnail));
            #endregion

            #region 删除图片
            string picture = item[Alias.Teacher.Picture].ToString();
            if (picture.StartsWith("/") && File.Exists(Manager.MapPath(picture)))
                File.Delete(Manager.MapPath(picture));
            #endregion

            Dal.Delete(id);
        }
        public DataTable List(int page, int pageSize, string condition, string sort)
        {
            if (CheckRight)
            {
                ValidateRight("List", Guid.Empty);
            }

            return Dal.List(page, pageSize, condition, sort);
        }
        public int GetRecordCount(string condition)
        {
            return Dal.GetRecordCount(condition);
        }

        public void IncrementHitCount(int id)
        {
            Dal.IncrementHitCount(id);
        }

        #region Properties
        private Dal.ITeacher Dal
        {
            get { return (Dal.ITeacher)DalBase; }
        }
        #endregion
    }
}
